package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.query.TravelCommentQuery;
import cn.wolfcode.luowowo.comment.repository.ITravelCommentRepository;
import cn.wolfcode.luowowo.comment.service.ITravelCommentService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * @author 猫教授
 * @date 2019/8/13 13:46
 */
@Service
public class TravelCommentServiceImpl implements ITravelCommentService {
    @Autowired
    private ITravelCommentRepository travelCommentRepository;
    @Autowired
    private MongoTemplate template;


    @Override
    public TravelComment save(TravelComment comment) {
        Query query = new Query();
        Criteria criteria = Criteria.where("travelId").is(comment.getTravelId());
        query.addCriteria(criteria);
        Long count = template.count(query, TravelComment.class);
        comment.setFloor(count.intValue() + 1);
        if (comment.getType()==1){
            Optional<TravelComment> op = travelCommentRepository.findById(comment.getRefComment().getId());
            comment.setRefComment(op.get());
        }
        travelCommentRepository.save(comment);
        return comment;

    }

    @Override
    public Long selectCountByTravelId(Long travelId) {
        //travelCommentRepository.

        //查询条件
        Query query = new Query();
        query.addCriteria(Criteria.where("travelId").is(travelId));
        return template.count(query, TravelComment.class);

    }

    @Override
    public List<TravelComment> selectCommentForList(Long id) {

        return travelCommentRepository.findAll();
    }

    @Override
    public List<TravelComment> selectForTop10() {
        Query query = new Query();
        query.with(PageRequest.of(0,10));
        query.with(Sort.by(Sort.Direction.DESC,"createTime"));
        List<TravelComment> list = template.find(query, TravelComment.class);
        return list;
    }

//    @Override
//    public Page<TravelComment> selectCommentForList(TravelCommentQuery qo) {
//        //首先先拼接查询条件
//        Query query = new Query();
//        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.by(Sort.Direction.ASC, "createTime"));
//        query.with(pageable);
//        if (qo.getTravelId()!=-1){
//            Criteria criteria = Criteria.where("travelId").is(qo.getTravelId());
//            query.addCriteria(criteria);
//        }
//        Long count = template.count(query, TravelComment.class);
//            if (count>0){
//
//            //查询结果
//            List<TravelComment> list = template.find(query,TravelComment.class,"strategy_comment");
//            return new PageImpl(list,pageable,count);
//        }else {
//            return new PageImpl(Collections.emptyList(),pageable,0);
//        }
//    }


}
